﻿using Sobiens.SPCamlStudio.Common;
using Sobiens.SPCamlStudio.Common.Extensions;
using Sobiens.SPCamlStudio.Entities;
using Sobiens.SPCamlStudio.Entities.Interfaces;
using Sobiens.SPCamlStudio.Entities.SharePoint;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sobiens.SPCamlStudio.UI.Controls
{
    /// <summary>
    /// Interaction logic for QueriesPanel.xaml
    /// </summary>
    public partial class QueriesPanel : UserControl, IQueriesPanel
    {
        public QueriesPanel()
        {
            InitializeComponent();
        }

        public IQueryPanel AddNewQueryPanel(Folder folder, ISiteSetting siteSetting)
        {
            if(this.QueryProject == null)
            {
                this.QueryProject = new QueryProjectObject();
                this.QueryProject.FolderName = Guid.NewGuid().ToString();
                this.QueryProject.Name = "New Project";
            }

            ClosableTab theTabItem = new ClosableTab();
            theTabItem.ID = Guid.NewGuid();
            string folderPath = ((SPFolder)folder).FolderPath;
            string[] values = folderPath.Split(new char[] { '/' }, StringSplitOptions.None);
            string folderName = values[values.Length - 1];
            string fileName = "CAMLQuery" + QueriesTabControl.Items.Count + " " + folderName + ".xml";
            theTabItem.Title = fileName;
            QueriesTabControl.Items.Add(theTabItem);
            theTabItem.Initialize(fileName, siteSetting, folder);
            theTabItem.Focus();
            return theTabItem;
        }

        public QueryProjectObject QueryProject { get; set; }

        public IQueryPanel ActiveQueryPanel
        {
            get
            {
                if (QueriesTabControl.SelectedItem != null)
                    return (IQueryPanel)QueriesTabControl.SelectedItem;

                return null;
            }
        }

        public List<IQueryPanel> QueryPanels
        {
            get
            {
                List<IQueryPanel> queryPanels = new List<IQueryPanel>();
                foreach (object item in QueriesTabControl.Items)
                {
                    queryPanels.Add(item as IQueryPanel);
                }

                return queryPanels;
            }
        }

        public IQueryPanel GetQueryPanel(Guid id) 
        {
            foreach(IQueryPanel queryPanel in this.QueryPanels)
            {
                if (queryPanel.ID.Equals(id) == true)
                    return queryPanel;
            }

            return null;
        }

        public void Save()
        {
            string folderPath = ConfigurationManager.GetInstance().GetProjectsFolder() + "\\" + this.QueryProject.FolderName;
            if (Directory.Exists(folderPath) == false)
                Directory.CreateDirectory(folderPath);

            this.QueryProject.QueryPanels = new List<QueryPanelObject>();
            foreach (IQueryPanel queryPanel in this.QueryPanels)
            {
                this.QueryProject.QueryPanels.Add(queryPanel.GetQueryPanel());
//                queryPanel.Save(folderPath);
            }
            SerializationManager.SaveConfiguration<QueryProjectObject>(this.QueryProject, folderPath + "\\QueryPanelProject.xml");

        }

        public void Load(string filename)
        {
            //_FileName = filename;
        }

    }
}
